package com.example.showlistexample;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import java.util.ArrayList;
import java.util.List;


public class RegisterActivity extends AppCompatActivity {
    private EditText account, password;
    private Button save;
    private Toolbar toolbar;
    private List<Person> list = new ArrayList<>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("注册");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        if(DbUtils.getLiteOrm() == null){
            DbUtils.createDb(this, "ShowList");
        }
        account = (EditText) findViewById(R.id.account);
        password = (EditText) findViewById(R.id.password);
        save = (Button) findViewById(R.id.save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Person person = new Person();
                person.setName(account.getText().toString());
                person.setPassword(password.getText().toString());
                if (account.getText().toString().isEmpty() || password.getText().toString().isEmpty()){
                    Toast.makeText(RegisterActivity.this, "用户名或者密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                list = DbUtils.getQueryByWhere(Person.class, "name", new String[]{account.getText().toString()});
                if (list.size() == 0){
                    DbUtils.insert(person);
                    Toast.makeText(RegisterActivity.this, "注册成功！", Toast.LENGTH_SHORT).show();
                    finish();
                }else {
                    Toast.makeText(RegisterActivity.this, "注册失败，用户名已存在", Toast.LENGTH_LONG).show();
                }

            }
        });

    }
}


